<?php
	session_start();
	if(!isset($_SESSION["jara_admin"])) {
		header("Location: ../login.php");
		exit;
	}
	if($_SESSION["jara_permissions"][2] == 0) {	
		header("Location: error.php");
		exit;
	}
	require_once("../include/templating_fns.php");
	require_once("../include/db_fns.php");
	jara_page_start("Users - Administrator CP");
	$result = jara_db_query("select * from jara_users order by username");
	$num_users = $result->num_rows;
?>
	<p><a href="add_user.php">Add a new user</a></p>
	<table border="1" class="admin-table">
		<tr>
			<th width="400"><strong>Username</strong></th>
			<th width="40"><strong>Posts</strong></th>
			<th width="60"><strong>Actions</strong></th>
		</tr>
<?php
	if($num_users == 0) {
?>
		<tr>
			<td colspan="4" align="center">There are no users, apparently.</td>
		</tr>
<?php
	}
	for($i = 0; $i < $num_users; $i++) {
		$row = $result->fetch_assoc();
		$num_posts_1 = jara_db_query("select * from jara_posts where author = '".$row["username"]."'");
		$num_posts = $num_posts_1->num_rows;
?>
		<tr>
			<td><?php echo $row["username"]; ?></td>
			<td><?php echo $num_posts; ?></td>
			<td>
				<a href="../profile.php?username=<?php echo $row["username"]; ?>" title="View Profile"><img src="images/jara_view.png" alt="View Profile" border="0" style="border: none" /></a>
				<a href="edit_user.php?id=<?php echo $row["id"]; ?>" title="Edit User"><img src="images/jara_edit.png" alt="Edit User" border="0" style="border: none" /></a>
				<a href="javascript:;" onclick="var q = confirm('Are you sure you want to delete this user?'); if(q) { window.location.href = 'delete_user.php?id=<?php echo $row["id"]; ?>' }" title="Delete User"><img src="images/jara_delete.png" alt="Delete Users" border="0" style="border: none" /></a>
			</td>
		</tr>
<?php
	}
?>
	</table>
<?php
	jara_page_end();
?>